package com.gdust.gkyx.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdust.gkyx.model.user.Leader;
import com.gdust.gkyx.model.user.User;
import com.gdust.gkyx.model.user.UserDelivery;
import com.gdust.gkyx.user.mapper.LeaderMapper;
import com.gdust.gkyx.user.mapper.UserDeliveryMapper;
import com.gdust.gkyx.user.mapper.UserMapper;
import com.gdust.gkyx.user.service.UserService;
import com.gdust.gkyx.vo.user.LeaderAddressVo;
import com.gdust.gkyx.vo.user.UserLoginVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * ClassName: UserServiceImpl
 * Package: com.gdust.gkyx.service.impl
 * Description:用户服务实现类，包含获取用户信息、团长信息以及用户登录信息的功能
 *
 * @Author syun
 * @Create 2025/1/27 20:28
 * @Version 1.0
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    // 用户提货点信息操作的Mapper
    @Autowired
    private UserDeliveryMapper userDeliveryMapper;

    // 团长信息操作的Mapper
    @Autowired
    private LeaderMapper leaderMapper;

    /**
     * //// 判断是否是第一次使用微信授权登录：如何判断？openId
     * @param openid 微信用户的唯一标识
     * @return
     */
    @Override
    public User getUserByOpenId(String openid) {
        // 使用LambdaQueryWrapper构造查询条件：通过openId查找用户
        User user = baseMapper.selectOne(
                new LambdaQueryWrapper<User>().eq(User::getOpenId, openid)
        );
        return user;
    }

    /**
     * //5 根据userId查询提货点和团长信息
     * @param userId 用户的唯一标识
     * @return
     */
    @Override
    public LeaderAddressVo getLeaderAddressByUserId(Long userId) {
        //根据userId查询用户默认的团长id
        UserDelivery userDelivery = userDeliveryMapper.selectOne(
                new LambdaQueryWrapper<UserDelivery>()
                        .eq(UserDelivery::getUserId, userId)// 用户ID匹配
                        .eq(UserDelivery::getIsDefault, 1)// 查询默认提货点
        );
        // 如果用户没有设置默认提货点，直接返回null
        if(userDelivery == null) {
            return null;
        }
        // 根据提货点信息中的团长ID查询团长详细信息
        //拿着上面查询团长id查询leader表查询团长其他信息
        Leader leader = leaderMapper.selectById(userDelivery.getLeaderId());
        // 将团长信息和提货点信息封装到LeaderAddressVo对象中
        //封装数据到LeaderAddressVo
        LeaderAddressVo leaderAddressVo = new LeaderAddressVo();
        // 复制Leader对象的属性到leaderAddressVo中
        BeanUtils.copyProperties(leader, leaderAddressVo);
        //设置leaderAddressVo特有的属性
        leaderAddressVo.setUserId(userId);// 设置用户ID
        leaderAddressVo.setLeaderId(leader.getId());// 设置团长ID
        leaderAddressVo.setLeaderName(leader.getName());// 设置团长姓名
        leaderAddressVo.setLeaderPhone(leader.getPhone());// 设置团长电话
        leaderAddressVo.setWareId(userDelivery.getWareId());// 设置提货点仓库ID
        leaderAddressVo.setStorePath(leader.getStorePath());// 设置团长门店路径
        return leaderAddressVo;
    }



    /**
     * //7 获取当前登录用户信息，
     * @param id 用户ID
     * @return
     */
    @Override
    public UserLoginVo getUserLoginVo(Long id) {
        // 根据用户ID查询用户基本信息
        User user = baseMapper.selectById(id);
        // 创建UserLoginVo对象用于封装返回值
        UserLoginVo userLoginVo = new UserLoginVo();
        userLoginVo.setUserId(id);// 设置用户ID
        userLoginVo.setNickName(user.getNickName());// 设置用户昵称
        userLoginVo.setPhotoUrl(user.getPhotoUrl());// 设置用户头像URL
        userLoginVo.setIsNew(user.getIsNew());// 设置是否为新用户
        userLoginVo.setOpenId(user.getOpenId());// 设置用户的微信OpenID

        // 查询用户默认的提货点信息
        UserDelivery userDelivery = userDeliveryMapper.selectOne(
                new LambdaQueryWrapper<UserDelivery>().eq(UserDelivery::getUserId, id)// 用户ID匹配
                        .eq(UserDelivery::getIsDefault, 1)// 查询默认提货点
        );
        // 如果存在默认提货点，设置团长ID和仓库ID
        if(userDelivery != null) {
            // 设置团长ID
            userLoginVo.setLeaderId(userDelivery.getLeaderId());
            // 设置仓库ID
            userLoginVo.setWareId(userDelivery.getWareId());
        } else {
            // 如果没有默认提货点，设置默认值
            // 默认团长ID
            userLoginVo.setLeaderId(1L);
            // 默认仓库ID
            userLoginVo.setWareId(1L);
        }
        return userLoginVo;
    }


}
